/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */     
#include "stm32f1xx_hal.h"
#include "sh1106.h"
#include "WinManager.h"
#include "bsp.h"
#include "my_log.h"
#include "WM.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
osThreadId defaultTaskHandle;
osThreadId led_taskHandle;
osThreadId keyScanHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
   
/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void const * argument);
void StartTask02(void const * argument);
void StartkeyScan(void const * argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */
       
  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of defaultTask */
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 512);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

  /* definition and creation of led_task */
  osThreadDef(led_task, StartTask02, osPriorityIdle, 0, 128);
  led_taskHandle = osThreadCreate(osThread(led_task), NULL);

  /* definition and creation of keyScan */
  osThreadDef(keyScan, StartkeyScan, osPriorityIdle, 0, 128);
  keyScanHandle = osThreadCreate(osThread(keyScan), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used 
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */

void lvgl_app_entry(void);
void StartDefaultTask(void const * argument)
{

  /* USER CODE BEGIN StartDefaultTask */
    SH1106_Init();
    //SH1106_Clear();
    //MainTask();
    lvgl_app_entry();
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the led_task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void const * argument)
{
  /* USER CODE BEGIN StartTask02 */
  /* Infinite loop */
  for(;;)
  {
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
    osDelay(500);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
    osDelay(500);
  }
  /* USER CODE END StartTask02 */
}

/* USER CODE BEGIN Header_StartkeyScan */
static const int keyTable[] = 
{
    GUI_KEY_END,
    GUI_KEY_UP,
    GUI_KEY_UP,
    GUI_KEY_UP,
    GUI_KEY_DOWN,
    GUI_KEY_DOWN,
    GUI_KEY_DOWN,
    GUI_KEY_LEFT,
    GUI_KEY_LEFT,
    GUI_KEY_LEFT,
    GUI_KEY_RIGHT,
    GUI_KEY_RIGHT,
    GUI_KEY_RIGHT,
    GUI_KEY_ENTER,
    GUI_KEY_ENTER,
    GUI_KEY_ENTER,
};
/**
* @brief Function implementing the keyScan thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartkeyScan */
void StartkeyScan(void const * argument)
{
  /* USER CODE BEGIN StartkeyScan */
    uint8_t keyCode = KEY_NONE;
    bsp_InitKey();
  /* Infinite loop */
  for(;;)
  {
      bsp_KeyScan();
      keyCode = bsp_GetKey();
      if(keyCode != KEY_NONE)
      {
          log_i("Key code: %d", (keyCode - 1) / 3);
          //GUI_SendKeyMsg(keyTable[keyCode], (keyCode % 3 == 2) ? 0 : 1);
      }
    osDelay(10);
  }
  /* USER CODE END StartkeyScan */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
     
/* USER CODE END Application */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
